package com.xy.order.service.impl;

import com.ruoyi.common.core.domain.home.Orders;
import com.ruoyi.common.core.domain.order.dto.OrderDetailDTO;
import com.ruoyi.common.core.domain.order.vo.SubmitPictures;
import com.ruoyi.common.core.mapper.order.OrdersMapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.xy.order.service.IOrdersService;
import com.xy.order.service.ISubmitPicturesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-16
 */
@Service
public class OrdersServiceImpl implements IOrdersService
{
    @Autowired
    private OrdersMapper ordersMapper;
    
    @Autowired
    private ISubmitPicturesService submitPicturesService;

    /**
     * 查询订单详情
     */
    @Override
    public List<OrderDetailDTO> findAllOrderDetails() {
        return ordersMapper.findAllOrderDetails();
    }

    /**
     * 查询订单
     * @param id 订单主键
     * @return
     */
    @Override
    public Orders selectOrdersById(String id) {
        return ordersMapper.selectOrdersById(id);
    }

    /**
     * 新增订单
     *
     * @param orders 订单
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders)
    {
        orders.setCreateTime(DateUtils.getNowDate());
        return ordersMapper.insertOrders(orders);
    }

    /**
     * 修改订单
     *
     * @param orders 订单
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders)
    {
        orders.setUpdateTime(DateUtils.getNowDate());
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteOrdersByIds(String[] ids)
    {
        // 先删除每个订单关联的审核照片
        for (String orderId : ids) {
            submitPicturesService.deleteSubmitPicturesByOrderId(Long.valueOf(orderId));
        }
        // 然后删除订单
        return ordersMapper.deleteOrdersByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteOrdersById(String id)
    {
        // 先删除该订单关联的审核照片
        submitPicturesService.deleteSubmitPicturesByOrderId(Long.valueOf(id));
        // 然后删除订单
        return ordersMapper.deleteOrdersById(id);
    }
}
